from app import datatype

#################################################
## Java 6
#################################################
def generate_stub_java6(arg_list, return_type):
	fun = lambda x:to_java_type(x[0]) + ' ' + x[1]
	signature = map(fun, arg_list)
	signature = ', '.join(signature)
	return_type = to_java_type(return_type)
	stub = """import java.util.*;

public class Solution {{
	public {0} solve({1}){{
		
	}}
}}
"""
	print stub
	print signature
	print return_type
	return stub.format(return_type, signature)

def generate_prog_java6(arg_list, user_prog, testcase_list):
	return 'stub impl'

def to_java_type(aType):
	start = aType.find('[')	
	if start != -1:
		return to_java_type(aType[:start]) + \
			aType[start:]

	if aType == 'Integer':
		return 'int'
	if aType == 'Boolean':
		return 'boolean'
	if aType == 'String':
		return 'String'

	raise Exception('Unknown type to Java6: ' + aType)

#################################################
## Python 2
#################################################
def generate_stub_python2(arg_list, return_type):
	signature = map((lambda x:x[1]), arg_list)
	signature = ', '.join(signature)
	stub = """def solve({0}):
	
"""
	return stub.format(signature)


def generate_prog_python2(arg_list, user_prog, testcase_list):
	prog = """import json
{0}
_jot_ans = {{}}
{1}
print(json.dumps(_jot_ans))
"""

	test_lines = ""
	for t in testcase_list:
		args = instantiate_python2(arg_list,t['entry'])
		args = ', '.join(args)
		line = "_jot_ans['t_{0}'] = solve({1})\n"
		test_lines += line.format(t['t_id'],args)

	return prog.format(user_prog, test_lines)

def instantiate_python2(arg_list, entry):
	def inst(aType, aVal):
		if aType[-2:] != '[]':
			if aType == 'String':
				return '"' + aVal + '"'
			else:
				return str(aVal) #boolean or integer
		# for arrays
		newType = aType[:-2]
		elements = [inst(newType, val) for val in aVal]
		return "[{0}]".format(', '.join(elements)) 

	return [inst(arg[0], entry[arg[1]]) for arg in arg_list]

